package com.smart.four.ioc.lifecycle.applicationcontext;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 * 车
 *
 * @author junrui.wang
 * @create 2018/4/18
 */
//管理bean生命周期的接口
public class Car implements BeanFactoryAware, BeanNameAware,
        InitializingBean, DisposableBean{
    private String brand;
    private String color;
    private int maxSpeed;

    private BeanFactory beanFactory;
    private String beanName;

    public Car() {
        System.out.println("运行Car的构造函数");
    }

    public Car(String brand, String color, int maxSpeed) {
        this.brand = brand;
        this.color = color;
        this.maxSpeed = maxSpeed;
    }

    public void introduce(){
        System.out.println("brand:"+brand+",color:"+color+",maxSpeed:"+maxSpeed);
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        System.out.println("调用setBrand()设置属性");
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    //BeanFactory接口方法
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("调用BeanFactoryAware.setBeanFactory()方法。");
        this.beanFactory = beanFactory;
    }

    //调用BeanNameAware接口方法
    public void setBeanName(String name) {
        System.out.println("调用BeanNameAware.setBeanName()方法。");
        this.beanName = name;

    }

    //调用DisposableBean接口方法
    public void destroy() throws Exception {
        System.out.println("调用DisposableBean.destroy()方法。");
    }

    //调用InitializingBean接口方法
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用InitializingBean.afterPropertiesSet()方法。");
    }

    //通过<bean>的init-method属性指定的初始化方法
    public void myInit(){
        System.out.println("调用init-method属性指定的初始化方法myInit(),将maxSpeed设置为240。");
        this.maxSpeed = 240;
    }

    //通过<bean>的destroy-method属性指定的销毁方法
    public void myDestroy(){
        System.out.println("调用destroy-method所指定的myDestroy()方法。");
    }
}
